/******
 * Javascript library for irods space audit services
 *
 * Author: Mike Conway - RENCI (www.renci.org)
 * Date: 07/22/2009
 */


/*
 * TODO: temporarily disabled this function
 * function initSpaceAudit() {
	$(document).ready(
			function() {
				$.get("irods.xml?_uri=//irods/get_irods_users_list", function(
						data) {
					var serviceData = data.substring(
							(data.indexOf("##START##") + 9), data
									.indexOf("##END##"));
					var irodsUsers = serviceData.split("|");
					$("#irodsUser").autocomplete(irodsUsers).focus();
				}, "text");
				$("#startDate").datepicker();
				$("#endDate").datepicker();
			});
}
*/

/**********************
 *  refresh the space audit table on user demand
 *  TODO: look at format of size display, and a few charts do not render when very small amounts
 *  	involved - mcc
 */

function getSpaceAudit() {

	$("#spaceAuditResult")
			.html(
					"<img src='images/loading.gif'>&nbsp;&nbsp;Fetching data from iRODS, please wait...");
	var output = "";
			$.get("irods.xml?_uri=//com.enginframe.irods.performance/get_space_by_user", function(data) {
				var coreData = data.substring((data.indexOf("##START##") + 9),
						data.indexOf("##END##"));
						
						
				// check for errors coming back from the AJAX call
				var errDat = checkAppError(coreData);
				if (errDat.status == "E") {
					//console.trace();
					//console.log("error in getSpaceAudit():" + errDat.errorMinor);
					throw errDat.errorMinor;
				} 

				var items = coreData.split("\n");
				var totalCount = 0;
				var totalSize = 0;

				// get total count and size for pie charts

				var parsedCount = 0;
				var parsedSize = 0;

				for ( var i = 0; i < (items.length); i++) {
					var tokens = items[i].split("|");

					if (tokens.length > 1) {
						parsedCount = parseInt(tokens[3]);
						parsedSize = parseInt(tokens[2]);
						totalCount += parsedCount;
						totalSize += parsedSize;
					}

				}

					output += "<div id='spaceDataContainer'><table cellpadding='5' cellspacing='0' id='tabularData' class='list-table'><thead>";
					output += "<tr class='lt-header'>";
					output += "<th class='lt-header-field'>User name</th>";
					output += "<th class='lt-header-field'>Total size</th>";
					output += "<th class='lt-header-field'>Total count</th>";
					output += "<th class='lt-header-field'></th>";
					output += "</tr></thead><tbody>";

					var sizePct = 0;

					for ( var i = 0; i < (items.length); i++) {
						var tokens = items[i].split("|");

						if (tokens.length > 1) {
							parsedSize = parseInt(tokens[2]);
							sizePct = (parsedSize / totalSize) * 100;
							// show 'something' if a very small amount of the total is for this user
							if (sizePct < 2) {
								sizePct = 2;
							}

							output += "<tr class='lt-row'><td class='lt-field' id='user_id_"
									+ trim(tokens[1]) +	"'>"
								+ tokens[0] + "</td>";
							output += "<td class=lt-field'>"
									+ tokens[2] + "</td>";
							output += "<td class='lt-field'>" + tokens[3]
									+ "</td>";
							output += "<td class='lt-chart-field'>"
									+ "<img src=\"/eastwood-1.1.0/chart?chs=300x120&amp;chd=t:"
									+ sizePct + "," + (100 - sizePct)
									+ "&amp;cht=p3&amp;chl="
									+ "User"
									+ "|Total\""
									+ " alt=\"Space Chart\" />"
									+ "</td></tr>";
						}
					}

					// finish the table rows
					output += "</tbody>";
					// add a footer line for totals  
					output += "<tfoot>";
					output += "<tr class='lt-row'><td class='lt-field'>Total:</td>";
					output += "<td class='lt-field'>"
						+ addCommas(totalSize) + "</td>";
					output += "<td class='lt-field'>" + addCommas(totalCount)
						+ "</td></tr>";
						
						
					// set the top-line totals with computed data
					$("#space_total_files").html(addCommas(totalCount));
					$("#space_total_space").html(addCommas(totalSize));

					// close out the table footer and the table
					output += "</foot></table></div>";
					$("#spaceAuditResult").html(output);
					$("#tabularData").dataTable( {
						"bAutoWidth" :true,
						"bSortClasses" :true,
						oLanguage : {
							"sSearch" :"Filter:"
						}, 
						"aoColumns": [
            						{ "sType": "string" },
							{ "sType": "numeric" },
							{   "sType": "numeric" },  
							null
						       ]
					});
				});

}
 
